import httpFetch from "share/httpFetch";
import { message } from "antd";
import config from "config";

const AirLineServer = function (context) {
  context.setState({ loading: true });
  const realmId = config.realmIds[location.host] || config.realmId;
  return {
    // 获取机票基础设置数据
    async getAirModelData(params) {
      try {
        const response = await httpFetch.get(
          "/open/user/api/config/v2/getFlightBaseConfig?realmId="+realmId,
          { ...params, ...{ configPage: true } },
          {
            source: "console",
          }
        );
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 保存机票基础设置数据
    async saveAirModelData(params) {
      try {
        const response = await httpFetch.post("/open/user/api/config/saveFlightBaseConfig?realmId="+realmId, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 获取机票询价配置
    async getAirInternationalData(oid) {
      try {
        const params = {
          companyOid: oid,
        };
        const response = await httpFetch.get("/open/user/api/config/getFlightRfpConfig?realmId="+realmId, params, { source: "control" });
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 保存机票询价配置
    async saveAirInternationalData(params) {
      try {
        const response = await httpFetch.post("/open/user/api/config/saveFlightRfpConfig?realmId="+realmId, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 获取机票供应商数据
    async getAddedSupplierSigns(params) {
      try {
        const response = await httpFetch.get("/open/airlines/api/flight/company/addedSupplierSigns?realmId="+realmId, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },
    async getSupportSuppliers() {
      try {
        const response = await httpFetch.get("/open/airlines/api/flight/vendor/supportSuppliers?realmId="+realmId, { complete: true });
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 保存机票供应商数据
    async saveAirSupplierData(params) {
      try {
        const response = await httpFetch.post("/open/airlines/api/flight/company/saveSupplierSigns?realmId="+realmId, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 获取机票管控配置数据
    async getAirControlData(params) {
      try {
        const response = await httpFetch.get(
          "/open/user/api/config/v2/getFlightControlConfig?realmId="+realmId,
          { ...params, ...{ configPage: true } },
          {
            source: "console",
          }
        );
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 保存机票管控配置数据
    async saveAirControlData(params) {
      try {
        const response = await httpFetch.post("/open/user/api/config/saveFlightControlConfig?realmId="+realmId, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 获取国际机票配置
    async getInternationalData(oid) {
      try {
        const params = {
          companyOid: oid,
        };
        const response = await httpFetch.get("/open/user/api/config/v2/getFlightBaseConfig?realmId="+realmId, params, {
          source: "console",
        });
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },
  };
};

const dealWithErrorMes = (err) => {
  message.error(`${err.response.data.message}`);
};

export default AirLineServer;
